package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.ProductSpecService;
import com.spzx.product.vo.ProductSpecVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品规格接口管理")
@RestController
@RequestMapping("/productSpec")
public class ProductSpecController extends BaseController {

    @Autowired
    private ProductSpecService productSpecService;

    @RequiresPermissions("product:productSpec:list")
    @Operation(summary = "分页查询商品规格")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpec productSpec) {
        startPage();
        List<ProductSpecVo> list = productSpecService.pageList(productSpec);
        return getDataTable(list);
    }

    @RequiresPermissions("product:productSpec:query")
    @Operation(summary = "获取商品规格详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ProductSpecVo vo = productSpecService.getInfo(id);
        return success(vo);
    }

    @Log(title = "商品规格", businessType = BusinessType.INSERT)
    @RequiresPermissions("product:productSpec:add")
    @Operation(summary = "新增商品规格")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated ProductSpec productSpec) {
        productSpecService.save(productSpec);
        return success();
    }

    @Log(title = "商品规格", businessType = BusinessType.UPDATE)
    @RequiresPermissions("product:productSpec:edit")
    @Operation(summary = "修改商品规格")
    @PutMapping
    public AjaxResult update(@RequestBody @Validated ProductSpec productSpec) {
        productSpecService.update(productSpec);
        return success();
    }

    @Log(title = "商品规格", businessType = BusinessType.DELETE)
    @RequiresPermissions("product:productSpec:remove")
    @Operation(summary = "删除商品规格")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable("ids") Long[] ids) {
        productSpecService.delete(ids);
        return success();
    }

    @RequiresPermissions("product:productSpec:query")
    @Operation(summary = "根据分类id获取商品规格列表")
    @GetMapping("/productSpecList/{categoryId}")
    public AjaxResult selectProductSpecListByCategoryId(@PathVariable Long categoryId) {
        List<ProductSpec> list = productSpecService.selectProductSpecListByCategoryId(categoryId);
        return success(list);
    }
}
